{include file="public/layout" /}
<body>
  <div class="layui-fluid">
      <div class="layui-row layui-col-space15">
          <div class="layui-col-md12">
            <div class="layui-card">
              <div class="layui-card-body" pad15>
                <div class="layui-form" wid100 lay-filter="">
                  <div class="layui-form-item">
                    <label class="layui-form-label"><b>*</b>字段标题</label>
                    <div class="layui-input-inline">
                     <input type="text" name="title" value="{$info.title|default=''}" lay-verify="required" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label"><b>*</b>字段名称</label>
                    <div class="layui-input-inline">
                        <input type="text" value="{$info.name|default=''}" name="name" lay-verify="required|check_name" placeholder="只允许字母、数字和下划线的任意组合" class="layui-input" onkeyup="this.value=this.value.replace(/[^0-9a-zA-Z_]/g,'');" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^0-9a-zA-Z_]/g,''));">
                    </div>
                    <div class="layui-input-inline layui-btn-container " style="width: auto;">
                      <div class="layui-form-mid layui-word-aux ey_helptips"></div>
                      <div class="layui-form-inline2 ey_helptips_txt">保持唯一性，不可与主表、附加表重复</div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label"><b>*</b>字段类型</label>
                    <div class="layui-input-inline">
                      <select name="dtype" id="dtype" lay-filter="dtype" lay-verify="check_dtype">
                        {volist name='$fieldtype_list' id='vo'}
                        <option value="{$vo.name}" data-ifoption="{$vo.ifoption|default=0}" {eq name="$info.dtype" value="$vo.name"}selected{/eq}>{$vo.title}</option>
                        {/volist}
                      </select>
                    </div>
                  </div>
                  <div class="layui-form-item none" id="dl_dfvalue">
                    <label class="layui-form-label" id="label_dfvalue">默认值</label>
                    <div class="layui-input-inline">
                        <textarea rows="5" cols="60" id="dfvalue" name="dfvalue" lay-verify="check_dfvalue" class="layui-textarea" placeholder="如果定义字段类型为下拉框、单选项、多选项时，此处填写被选择的项目(用“,”分开，如“男,女,人妖”)。" style="height:76px;">{$info.dfvalue|default=''}</textarea>
                    </div>
                    <div class="layui-input-inline layui-btn-container " style="width: auto;">
                      <div class="layui-form-mid layui-word-aux ey_helptips"></div>
                      <div class="layui-form-inline2 ey_helptips_txt">如果定义字段类型为下拉框、单选项、多选项时，此处填写被选择的项目(用“,”分开，如“男,女,人妖”)。</div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">提示文字</label>
                    <div class="layui-input-inline">
                      <textarea name="remark" class="layui-textarea" placeholder="问号提示文字">{$info.remark|default=''}</textarea>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="hidden" name="channel_id" id="channel_id" value="{$info.channel_id|default='-99'}">
                        <input type="hidden" name="id" id="id" value="{$info.id|default=''}">
                        <input type="hidden" name="old_name" id="old_name" value="{$info.name|default=''}">
                        <button class="layui-btn" lay-submit lay-filter="formSubmit">确认提交</button>
                    </div>
                  </div>
              </div>
            </div>
          </div>
        </div>
  </div>

  <script>
  layui.config({
    base: '__SKIN__/' //静态资源所在路径
    ,version: '{$version}'
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'set'], function(){
    var $ = layui.$
    ,layer = layui.layer
    ,form = layui.form;

    var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
      
    dtype_change($('select[name=dtype]'));

    form.verify({
      check_name: function(value, item){ //value：表单的值、item：表单的DOM对象
        var name = $('input[name="name"]').val();
        if (/^[0-9]+$/.test(name)) {
            return '字段名称不能纯数字！';
        } else if (/^((_+)|(\d+))$/.test(value) || !/^(\w+)$/.test(value)) {
            return '字段名称格式不正确！';
        }
      }
      ,check_dtype: function(value, item){
        if ('' == value || undefined == value) {
            return '请选择字段类型！';
        }
      }
      ,check_dfvalue: function(value, item){
        var dtype_value = $('#dtype').find('option:selected').val();
        if (0 > $.inArray(dtype_value, ['region','region_db'])){
            var ifoption = $('#dtype').find('option:selected').data('ifoption');
            if (1 == ifoption) {
                if ($.trim($('#dfvalue').val()) == '') {
                    $('#dfvalue').focus();
                    return '默认值不能为空！';
                }
            }
        }
      }
    }); 

    // 字段类型
    form.on('select(dtype)', function(data){
        dtype_change(data.elem);
    });

    //监听提交
    form.on('submit(formSubmit)', function(data){
        var load = layer_loading();
        data.field._ajax = 1;
        data.field.typeids = $('#typeid').val();
        $.ajax({
            type : 'post',
            url : "{:url('Field/arctype_edit')}",
            data : data.field,
            dataType : 'json',
            success : function(res){
                layer.close(load); //关闭loading
                if(res.code == 1){
                    layer.msg(res.msg, {icon: 1, time: 1000}, function(){
                        parent.gourl(res.url);
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.close(load); //关闭loading
                showErrorAlert();
            }
        });
        return false;
    });

    function dtype_change(obj) {
        var dtype = $(obj).val();
        var ifoption = $(obj).find('option:selected').data('ifoption');
        if (0 <= $.inArray(dtype, ['datetime','switch','img','imgs','files'])) {
            $('#dl_dfvalue').hide();
        } else {
            if (1 == ifoption) {
                $('#label_dfvalue').html('<b>*</b>默认值');
            } else {
                $('#label_dfvalue').html('默认值');
            }
            $('#dl_dfvalue').show();
        }
    }

  });

  </script>
{include file="public/footer" /}